package value

import (
	"fmt"
	"reflect"

	"github.com/gookit/color"
)

type RR struct {
	Index   int    `json:"index" el:"el_index"`
	Title   string `json:"title" el:"el_title"`     //Offset: 8
	Age     int    `json:"age" el:"el_age"`         // Offset: 24 -8 =16
	private int    `json:"private" el:"el_private"` // Offset: 24 -8 =16
}

func (r RR) privateMethod() {
	fmt.Println(r.Index, r.Title, r.private)
}
func (r RR) ExportMethod(x int) {
	fmt.Println("ExportMethod", r.Index, r.Title, x)
}

func (r *RR) PointHandle(handle func() string) {
	fmt.Println(r.Index, r.Title, handle())
}

func Structx(x any) {
	v := reflect.ValueOf(x)
	switch v.Kind() {
	case reflect.Struct:
		t := v.Type()
		color.Blueln("Type().Kind:", t.Kind())
		color.Blueln("Type().PkgPath:", t.PkgPath())
		color.Blueln("Type().Comparable:", t.Comparable())
		color.Blueln("Type().NumMethod:", t.NumMethod())

		color.Redln("======NumMethod===", t.NumMethod())
		for i := 0; i < t.NumMethod(); i++ {
			method := t.Method(i)
			name := method.Name
			color.Cyanln("Name:", name, "PkgPath:", method.PkgPath, "Index:", method.Index, "Type:", method.Type, "Func:", method.Func, "IsExported", method.IsExported())
			color.Yellowln("Func CanAddr:", method.Func.CanAddr(), "Func CanSet:", method.Func.CanSet(), "Func CanComplex:", method.Func.CanComplex(), "Func CanFloat:", method.Func.CanFloat(), "Func CanInt:", method.Func.CanInt(), "Func CanInterface:", method.Func.CanInterface(), "Func CanUint:", method.Func.CanUint(), "Func IsNil:", method.Func.IsNil(), "Func IsValid:", method.Func.IsValid(), "Func IsZero:", method.Func.IsZero())
			fmt.Println("call", v.MethodByName(name).Call([]reflect.Value{reflect.ValueOf(1)}))
		}
		color.Redln("======NumField===", v.NumField())
		for i := 0; i < v.NumField(); i++ {
			field := t.Field(i)
			color.Yellowln("Type:", field.Type, "Title:", field.Name, "PkgPath:", field.PkgPath, "Type:", field.Type, "Tag:", field.Tag, "Offset:", field.Offset, "Index:", field.Index, "Anonymous:", field.Anonymous, "IsExported:", field.IsExported())
			color.Greenln(field.Tag.Lookup("el"))
			color.Cyanln(field.Tag.Lookup("els"))
		}
	default:
		panic("not struct")
	}

}
